package com.chinatelecom.ifree.business.service.order.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.chinatelecom.ifree.business.dao.order.SystemConfigDao;
import com.chinatelecom.ifree.business.domain.base.BoolObjResult;
import com.chinatelecom.ifree.business.domain.base.BoolResult;
import com.chinatelecom.ifree.business.domain.order.SystemConfig;
import com.chinatelecom.ifree.business.service.order.SystemConfigService;
import com.chinatelecom.ifree.business.util.BaseResult;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {
	private static transient Logger logger = LoggerFactory.getLogger(SystemConfigServiceImpl.class);
	@Resource
	private SystemConfigDao systemConfigDao;

	@Override
	public BoolObjResult getSystemConfigByKey(String key) {
		if (logger.isInfoEnabled())
			logger.info("key=============key={}", key);
		BoolObjResult result = new BoolObjResult();
		result.setResult(false);
		try {
			SystemConfig systemConfig = systemConfigDao.getSystemConfigByKey(key);
			result.setResult(true);
			result.setObj(systemConfig);
		} catch (Exception e) {
			logger.error("getSystemConfigByKey", e);
			result.setCode(BaseResult.SYSTEM_QUERY_CONFIG_ERROR.getCode());
			result.setMsg(BaseResult.SYSTEM_QUERY_CONFIG_ERROR.getMsg());
		}
		return result;
	}

	@Override
	public BoolResult updateSystemConfig(String key, String content) {
		BoolResult result = new BoolResult();
		result.setResult(false);
		try {
			boolean flag = systemConfigDao.updateAppendSystemConfig(key, content);
			result.setResult(flag);
		} catch (Exception e) {
			logger.error("updateSystemConfig", e);
			result.setCode(BaseResult.SYSTEM_SETTING_CONFIG_ERROR.getCode());
			result.setMsg(BaseResult.SYSTEM_SETTING_CONFIG_ERROR.getMsg());
		}
		return result;
	}

	@Override
	public BoolResult addOrUpdateSystemConfig(String key, String content) {
		BoolResult result = new BoolResult();
		result.setResult(false);
		try {
			boolean flag = systemConfigDao.addOrUpdateSystemConfig(key, content);
			result.setResult(flag);
		} catch (Exception e) {
			logger.error("addOrUpdateSystemConfig", e);
			result.setCode(BaseResult.SYSTEM_SETTING_CONFIG_ERROR.getCode());
			result.setMsg(BaseResult.SYSTEM_SETTING_CONFIG_ERROR.getMsg());
		}
		return result;
	}
}
